package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.PhysicalDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = PhysicalService.class)
@Transactional
public class PhysicalServiceImpl implements PhysicalService {
    @Autowired
    private PhysicalDao physicalDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private JedisPool jedisPool;
    @Override
    public void deleteById(Integer id) {

        if (id!=null){
            physicalDao.deleteById(id);
//            String file=physicalDao.findFileById(id);
//            if (file!=null){
//                QiniuUtils.deleteFileFromQiniu(file);
//            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用分页助手插件实现分页查询
        PageHelper.startPage(currentPage,pageSize);
        Page<Map> page = physicalDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        if (ids!=null && ids.length>0){
            for (Integer id : ids) {
                physicalDao.deleteById(id);
            }
        }
    }

    @Override
    public void add(Map map) throws RuntimeException{

        String fileNumber = (String) map.get("fileNumber");
        Integer memberId = memberDao.findByFileNumber(fileNumber);

        Map<String,Object> map1=new HashMap();
        String checkDate = (String) map.get("checkDate");

        map1.put("memberId",memberId);
        map1.put("orderDate",checkDate);
        Integer id =physicalDao.findById(map1);
        if (id==null){
            Map<String,Object> map2=new HashMap();
            String file = (String) map.get("file");
            String uploadStatus;
            if (file!=null&&file.length()>0){
                uploadStatus="报告已上传";
                String uploadTime = getTime(new Date());
                map2.put("uploadTime",uploadTime);
                map2.put("uploadStatus",uploadStatus);
                map2.put("checkDate",checkDate);

                map2.put("memberId",memberId);
                map2.put("file",file);
                physicalDao.add(map2);
                //完成数据库操作后需要将图片名称保存到redis
                jedisPool.getResource().sadd("PDF_DB",file);
            }else {
                uploadStatus="报告未上传";
                map2.put("uploadStatus",uploadStatus);
                map2.put("checkDate",checkDate);
                map2.put("memberId",memberId);
                map2.put("file",file);
                physicalDao.add(map2);
            }
        }else {
            throw new RuntimeException("请勿重复添加");
        }


    }

    @Override
    public void edit(Map map) {
        String file = (String) map.get("file");
        if (file!=null&&file.length()>0){
            Integer id = (Integer) map.get("id");
            String uploadStatus="报告已上传";
            Map map1=new HashMap();
            map1.put("file",file);
            map1.put("id",id);
            map1.put("uploadStatus",uploadStatus);
            String uploadTime = getTime(new Date());
            map1.put("uploadTime",uploadTime);
            physicalDao.edit(map1);
            Date date = new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm");
            String format = sdf.format(date);
        }
    }

    public String getTime(Date date){

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm");
        return sdf.format(date);
    }

    public List<Map> findAll(){
        //返回后台查到的数据（数据已经封装到Map里，直接返回就ok）
        return memberDao.findAll();
        //        List<Map> list = new ArrayList<>();
       /* for (Map m : ms) {

           String label = m.getName();
           String value = m.getFileNumber();


            Map map = new HashMap<>();
            map.put("label",label);
            map.put("value",value);

            list.add(map);

        }*/



    }

    @Override
    public List<Map> findReportById(Integer id) {
        List<Map> reports = physicalDao.findReportById(id);
//        for (Map report : reports) {
//            String uploadTime = (String) report.get("uploadTime");
//
//        }
        return reports;
    }

    //报告上传统计
    @Override
    public List<Integer> findPhysicalCountByMouth(List<String> result) {
        List<Integer> counts = new ArrayList<>();
        for (String dataString : result) {
            String month = dataString.replace(".", "-");
            Integer count = physicalDao.findPhysicalCountByMouth(month);
            counts.add(count);
        }
        return counts;
    }
}
